package cassandrawindow;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

/**
 *
 * @author quyenb
 */
public class HandleImportData {
    //public static final String PATH_ = "/home/bigdata/cpc/logOutFile/";
    public static final String PATH_ = "/home/bigdata/cpc/logOutFile/";
    //public static final String PATH_ = "/home/bigdata/service_cpc/banhourly_scipt/logOutFile/";
    //public static final String PATH_ = "E:\\\\";
    public String outToCsvDomain(String allDataString) throws IOException {
        Date date = new Date();
        //String fileName = PATH_+"v3Domain_" + date.getTime() + ".csv";
        String fileName = PATH_+"v3Domain_" + date.getTime() + ".csv";
        
        FileWriter fw = new FileWriter(fileName);
        //FileWriter fw = new FileWriter(fileName);
        PrintWriter pw = new PrintWriter(fw);
        String preDefineData = allDataString.replace("(", "").replace(")", "").replace("'", "");
        String[] dataArr = preDefineData.split(",");
        for (int i = 0; i < dataArr.length; i++) {
            if (i % 15 == 0 && i > 0) {
                pw.print("\n");
            }
            pw.print(dataArr[i]);
            pw.print(",");
        }
        pw.println();

        pw.flush();
        //Close the Print Writer
        pw.close();
        //Close the File Writer
        fw.close();
        return fileName;
    }
     
    public String outToCsvBanner(String allDataString) throws IOException {
        Date date = new Date();
        String fileName = PATH_ +"v3Banner_" + date.getTime() + ".csv";
        //FileWriter fw = new FileWriter("E:\\" + fileName);
        FileWriter fw = new FileWriter(fileName);
        PrintWriter pw = new PrintWriter(fw);
        String preDefineData = allDataString.replace("(", "").replace(")", "").replace("'", "");
        String[] dataArr = preDefineData.split(",");
        for (int i = 0; i < dataArr.length; i++) {
            if (i % 15 == 0 && i > 0) {
                pw.print("\n");
            }
            pw.print(dataArr[i]);
            pw.print(",");
        }
        pw.println();

        pw.flush();
        //Close the Print Writer
        pw.close();
        //Close the File Writer
        fw.close();
        return fileName;
    }

    public String outToCsvCampaign(String allDataString) throws IOException {
        Date date = new Date();
        String fileName = PATH_ +"v3Cam_" + date.getTime() + ".csv";
        //FileWriter fw = new FileWriter("E:\\" + fileName);
        FileWriter fw = new FileWriter(fileName);
        PrintWriter pw = new PrintWriter(fw);
        String preDefineData = allDataString.replace("(", "").replace(")", "").replace("'", "");
        String[] dataArr = preDefineData.split(",");
        for (int i = 0; i < dataArr.length; i++) {
            if (i % 12 == 0 && i > 0) {
                pw.print("\n");
            }
            pw.print(dataArr[i]);
            pw.print(",");
        }
        pw.println();

        pw.flush();
        //Close the Print Writer
        pw.close();
        //Close the File Writer
        fw.close();
        return fileName;
    }

    public String outToCsvCampaignHourly(String allDataString) throws IOException {
        Date date = new Date();
        String fileName = PATH_ +"v3CamHourly_" + date.getTime() + ".csv";
        //FileWriter fw = new FileWriter("E:\\" + fileName);
        FileWriter fw = new FileWriter(fileName);
        PrintWriter pw = new PrintWriter(fw);
        String preDefineData = allDataString.replace("(", "").replace(")", "").replace("'", "");
        String[] dataArr = preDefineData.split(",");
        for (int i = 0; i < dataArr.length; i++) {
            if (i % 5 == 0 && i > 0) {
                pw.print("\n");
            }
            pw.print(dataArr[i]);
            pw.print(",");
        }
        pw.println();

        pw.flush();
        //Close the Print Writer
        pw.close();
        //Close the File Writer
        fw.close();
        return fileName;
    }

    public String outToCsvBannerHourly(String allDataString) throws IOException {
        Date date = new Date();
        String fileName = PATH_ +"v3BanHourly_" + date.getTime() + ".csv";
        //FileWriter fw = new FileWriter("E:\\" + fileName);
        FileWriter fw = new FileWriter(fileName);
        PrintWriter pw = new PrintWriter(fw);
        String preDefineData = allDataString.replace("(", "").replace(")", "").replace("'", "");
        //String preDefineData = allDataString.replace("(", "").replace(")", "");
        String[] dataArr = preDefineData.split("_");
        for (int i = 0; i < dataArr.length; i++) {
            if (i % 4 == 0 && i > 0) {
                pw.print("\n");
            }
            pw.print(dataArr[i]);
            pw.print(";");
        }
        pw.println();

        pw.flush();
        //Close the Print Writer
        pw.close();
        //Close the File Writer
        fw.close();
        return fileName;
    }

    public String outToCsvZone(String allDataString) throws IOException {
        Date date = new Date();
        String fileName = PATH_ +"v3Zone_" + date.getTime() + ".csv";
        //FileWriter fw = new FileWriter("E:\\" + fileName);
        FileWriter fw = new FileWriter(fileName);
        PrintWriter pw = new PrintWriter(fw);
        String preDefineData = allDataString.replace("(", "").replace(")", "").replace("'", "");
        String[] dataArr = preDefineData.split(",");
        for (int i = 0; i < dataArr.length; i++) {
            if (i % 10 == 0 && i > 0) {
                pw.print("\n");
            }
            pw.print(dataArr[i]);
            pw.print(",");
        }
        pw.println();

        pw.flush();
        //Close the Print Writer
        pw.close();
        //Close the File Writer
        fw.close();
        return fileName;
    }
    
    public String outToCsvcamIp(String allDataString) throws IOException {
        Date date = new Date();
        String fileName = PATH_ +"v3camIp_" + date.getTime() + ".csv";
        //FileWriter fw = new FileWriter("E:\\" + fileName);
        FileWriter fw = new FileWriter(fileName);
        PrintWriter pw = new PrintWriter(fw);
        String preDefineData = allDataString.replace("(", "").replace(")", "").replace("'", "");
        String[] dataArr = preDefineData.split(",");
        for (int i = 0; i < dataArr.length; i++) {
            if (i % 3 == 0 && i > 0) {
                pw.print("\n");
            }
            pw.print(dataArr[i]);
            pw.print(",");
        }
        pw.println();

        pw.flush();
        //Close the Print Writer
        pw.close();
        //Close the File Writer
        fw.close();
        return fileName;
    }

    public String outToCsvPromotions(String allDataString) throws IOException {
        Date date = new Date();
        String fileName = PATH_ +"promotion_" + date.getTime() + ".csv";
        //FileWriter fw = new FileWriter("E:\\" + fileName);
        FileWriter fw = new FileWriter(fileName);
        PrintWriter pw = new PrintWriter(fw);
        String preDefineData = allDataString.replace("(", "").replace(")", "").replace("'", "");
        String[] dataArr = preDefineData.split(",");
        for (int i = 0; i < dataArr.length; i++) {
            if (i % 13 == 0 && i > 0) {
                pw.print("\n");
            }
            pw.print(dataArr[i]);
            pw.print(",");
        }
        pw.println();

        pw.flush();
        //Close the Print Writer
        pw.close();
        //Close the File Writer
        fw.close();
        return fileName;
    }
    
    
    public void loadToV3DomainDateFromFile(String filePath, Connection connection) throws Exception {
        String query = "LOAD DATA LOCAL INFILE '" + filePath + "'" +
                " REPLACE INTO TABLE v3_domain_date FIELDS TERMINATED BY ',' " +
                "LINES TERMINATED BY ',\\n' (campaignid, bannerid,domain," +
                " totalclick,realclick,totalview,viewcharge, @domaincrcRaw," +
                "partnerid,istarget,domaingroup,@domaingroupRaw,@dtStr,preoder,money)" +
                " SET domaingroupcrc=crc32(@domaingroupRaw), dt=date(@dtStr)," +
                " domaincrc=crc32(@domaincrcRaw)";
        Statement statement = (Statement) connection.createStatement();
        statement.executeQuery(query);
    }
    
    

}
